$(function() {
    // 当前页数
    var page = 1;

    // 每页展示的条数
    var pageSize = 5;

    // 待计算的总页数
    var totalpage;

    // 是否上下架
    var statu;

    // 库存
    var num;

    // 归属品牌
    var brandId;

    $.ajax({
        url: '/product/queryProductDetailList',
        type: 'get',
        data: {
            page: page,
            pageSize: pageSize
        },
        success: function(res) {

            console.log(res);

            totalpage = Math.ceil(res.total / pageSize);

            statu = res.statu;

            num = res.num;

            brandId = res.brandId;

            var html = template('product', res);

            $('.table').html(html);
        }
    });



    $('.table').on('click', '#btns', function() {
        $(this).addClass('btn-success').removeClass('btn-danger');
        var statu = $(this).attr('statu');
        var id = $(this).attr('data-id');
        // console.log(id)
        $.ajax({
            url: '/product/updateProduct',
            type: 'POST',
            data: {
                statu: statu == 1 ? 0 : 1
            },
            success: function(res) {
                console.log(res);
            }
        });
    })



    $('#next').on('click', function() {
        page++;
        if (page > totalpage) {

            page = totalpage;

            alert('抱歉客官，给不了你更多');

            return;
        }
        $.ajax({
            url: '/product/queryProductDetailList',
            type: 'get',
            data: {
                page: page,
                pageSize: pageSize
            },
            success: function(res) {
                var html = template('product', res);
                $('.table').html(html);

            }
        });
    });


    $('#prev').on('click', function() {
        page--;
        if (page < 1) {
            page = 1;
            alert('抱歉客官，已经到顶了');

            return;
        }
        $.ajax({
            url: '/product/queryProductDetailList',
            type: 'get',
            data: {
                page: page,
                pageSize: pageSize
            },
            success: function(res) {
                var html = template('product', res);
                $('.table').html(html);
            }
        });
    });





    $.ajax({
        url: '/category/querySecondCategoryPaging',
        type: 'get',
        data: {
            page: 1,
            pageSize: 50,
        },
        success: function(res) {
            // console.log(res);
            var html = template('productList', res);

            $('#productName').html(html);

        }
    });

    var pic = [];

    var pics = '';

    // 上传图片
    $('#fileupload').fileupload({
        dataType: 'json',
        done: function(e, data) {

            // console.log(data.result.picAddr);

            $('.pics').attr('src', data.result.picAddr);

            pic.push(data.result);
        }
    });


    $('#addProduct').on('click', function() {

        var proName = $.trim($("[name='proName']").val());

        var proDesc = $.trim($("[name='proDesc']").val());

        var oldPrice = $.trim($("[name='oldPrice']").val());

        var price = $.trim($("[name='price']").val());

        var size = $.trim($("[name='size']").val());

        $.ajax({
            url: '/product/addProduct',
            type: 'post',
            data: {
                proName: proName,
                oldPrice: oldPrice,
                price: price,
                proDesc: proDesc,
                size: size,
                statu: statu,
                num: num,
                brandId: brandId,
                pic: pic
            },
            success: function(res) {

                if (res.success) {

                    location.reload();

                } else

                    alert(res.message);

            }
        });

    });

});